﻿namespace FallingWordsGame
{
    using System;
    using System.Collections.Generic;
    using System.Linq;

    /// <summary>
    /// Makes All the changes on the words of the game 
    /// </summary>
    public static class WordManipulator
    {

        public static bool IsCurrentSpace(Word word)
        {
            char currentLower = word.Text.ToList().FirstOrDefault(x => char.IsLower(x) || x == ' ');
            return currentLower == ' ' ? true : false;
        }

        /// <summary>
        /// Changes the first occurance of the ' ' Symbol to the '@'
        /// </summary>
        /// <param name="word">reference of word object</param>
        public static void ChangeFirstSpace(Word word)
        {
            int firstSpaceIndex = word.Text.ToList().IndexOf(' ');

            if (firstSpaceIndex > 0)
            {
                word[firstSpaceIndex] = '@';
            }
        }

        /// <summary>
        /// Checks if the first not typed letter of the word matches the inputLetter
        /// </summary>
        /// <param name="word">word that we are trying to type in</param>
        /// <param name="inputLetter">char entered on the keyboard</param>
        /// <returns></returns>
        public static bool MatchFirstLowerChar(Word word, char inputLetter)
        {
            char firstLowerLetter = word.Text.ToList().FirstOrDefault(x => char.IsLower(x) || x == ' ');

            return firstLowerLetter == inputLetter;
        }

        /// <summary>
        /// Makes the first occurance of lower letter to upper, whitespace to '@'
        /// </summary>
        /// <param name="word">Word reference</param>
        public static void MakeFirstLowerToUpperChar(Word word)
        {
            char toChangeLetter = word.Text.ToList().FirstOrDefault(x => char.IsLower(x) || x == ' ');
            int index = word.Text.ToList().IndexOf(toChangeLetter);

            word[index] = char.IsLetter(toChangeLetter) ? char.ToUpper(toChangeLetter) : '@';
        }

        public static void ToLower(Word word)
        {
            for (int i = 0; i < word.Length; i++)
            {
                if (word[i] == '@')
                {
                    word[i] = ' ';
                }
                else
                {
                    word[i] = char.ToLower(word[i]);
                }
            }
        }
    }
}
